$(function(){
	var uploadLimit = 5;

	setTimeout(function(){
		$('#file_upload').uploadify({
			fileSizeLimit: '5MB',
			queueSizeLimit: uploadLimit,
			width: 180,
			height: 180,
			buttonClass: 'file_btn',
			buttonText: '',
			swf: config.uploadSwfUrl,
			fileTypeDesc : 'Image Files',
			fileTypeExts : '*.gif; *.jpg; *.png',
			uploader: config.uploadUrl,
			itemTemplate: '<div id="${fileID}" class="uploadify-queue-item">\
				<div class="uploadify-progress">\
					<div class="uploadify-progress-bar"></div>\
				</div>\
			</div>',
			overrideEvents: ['onSelect', 'onSelectError', 'onUploadComplete', 'onUploadSuccess'],
			onFallback: function(){
				$(this).replaceWith('<div class="upload-fall">请检查您的浏览器是否禁用了flash插件或是否安装flash插件，<br>flash插件下载地址：<a href="http://www.skycn.com/soft/appid/8.html" target="_blank">http://www.skycn.com/soft/appid/8.html</a></div>');
			},
			onSelect: function(file){
				var settings = this.settings,
					itemHTML = settings.itemTemplate,
					itemData = {
						'fileID'     : file.id,
						'instanceID' : settings.id,
						'fileName'   : file.name,
						'fileSize'   : Math.round(file.size / 1024)
					};

				for (var d in itemData) {
					itemHTML = itemHTML.replace(new RegExp('\\$\\{' + d + '\\}', 'g'), itemData[d]);
				}

				if($('.uploadify-queue-item').length < uploadLimit){
					
					$('#' + settings.queueID).append(itemHTML);
				}else{
					alert('最多上传'+ uploadLimit + '张图片');
					$('#' + settings.id).uploadify('cancel', file.id);
				}

			},
			onSelectError: function(file, errorCode, errorMsg){
				if(errorCode == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT){
					alert('"'+ file.name +'"超过大小限制，最大不超过'+ this.settings.fileSizeLimit + '！');
				}
			},
			onUploadComplete: function(file){
				var settings = this.settings,
				swfuploadify = this;

				if (settings.removeCompleted) {
					switch (file.filestatus) {
						case SWFUpload.FILE_STATUS.COMPLETE:
							setTimeout(function() { 
								if ($('#' + file.id)) {
									swfuploadify.queueData.queueSize   -= file.size;
									swfuploadify.queueData.queueLength -= 1;
									delete swfuploadify.queueData.files[file.id]
								}
							}, settings.removeTimeout * 1000);
							break;
						case SWFUpload.FILE_STATUS.ERROR:
							if (!settings.requeueErrors) {
								setTimeout(function() {
									if ($('#' + file.id)) {
										swfuploadify.queueData.queueSize   -= file.size;
										swfuploadify.queueData.queueLength -= 1;
										delete swfuploadify.queueData.files[file.id];
									}
								}, settings.removeTimeout * 1000);
							}
							break;
					}
				} else {
					file.uploaded = true;
				}
			},
			onUploadSuccess: function(file, data){
				var data = typeof data === 'string' ? $.parseJSON(data) : data,
					uploadItem = $('#' + file.id);

				if(data.status == 0){
					uploadItem.css('background-image', 'url('+ data.url +')').addClass('upload-success').html('<i class="upload-del"></i><input type="hidden" name="imgs[]" value="'+ data.url +'" />');

					if($('.upload-success').length >= uploadLimit){
						$('#uploadMask').show();
					}
					if($('.uploadify-queue-item').size()>4){
						$('#file_upload').css('width',0);
					};
				}
			}
		});

		$('#file_upload-queue').on('click', '.upload-del', function(){
			$(this).parent().remove();
			$('#file_upload').css('width','200px');
			$('#uploadMask').hide();
		});

		$('#file_upload').append('<div id="uploadMask" class="uploadMask"></div>');
		$('#uploadMask').on('click', function(){
			alert('最多上传'+ uploadLimit + '张图片');
		});

	}, 0);
})
	